import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName: "welecome" */ '../components/Login.vue')
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName: "welecome" */ '../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "welecome" */ '../components/Welcome.vue')
/* user related */
// import Users from '../components/user/Users.vue'
const Users = () => import(/* webpackChunkName: "user" */ '../components/user/Users.vue')
/* right related */
// import Rights from '../components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName: "right" */ '../components/power/Rights.vue')
// import Roles from '../components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName: "right" */ '../components/power/Roles.vue')
/* goods related */
// import Categories from '../components/goods/Categories.vue'
const Categories = () => import(/* webpackChunkName: "goods" */ '../components/goods/Categories.vue')
// import Params from '../components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "goods" */ '../components/goods/Params.vue')
// import List from '../components/goods/List.vue'
const List = () => import(/* webpackChunkName: "goods" */ '../components/goods/List.vue')
// import Add from '../components/goods/Add.vue'
const Add = () => import(/* webpackChunkName: "goods" */ '../components/goods/Add.vue')
/* order related */
// import Orders from '../components/order/Orders.vue'
const Orders = () => import(/* webpackChunkName: "order" */ '../components/order/Orders.vue')
/* report related */
// import Reports from '../components/report/Reports.vue'
const Reports = () => import(/* webpackChunkName: "report" */ '../components/report/Reports.vue')

Vue.use(VueRouter)

const routes = [
  /* 主页：登录页面 */
  {
    path: '/',
    redirect: '/login'
  },
  /* /login：登录 */
  {
    path: '/login',
    component: Login
  },
  /* home, login success to */
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      }, {
        path: '/roles',
        component: Roles
      }, {
        path: '/rights',
        component: Rights
      }, {
        path: '/goods',
        component: List
      }, {
        path: '/goods/add',
        component: Add
      }, {
        path: '/params',
        component: Params
      }, {
        path: '/categories',
        component: Categories
      }, {
        path: '/orders',
        component: Orders
      }, {
        path: '/reports',
        component: Reports
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

/* mount route navigation guard, for: not login, login first */
router.beforeEach((to, from, next) => {
  /* to: will to path
   * form: which path to come from
   * next: it's func, if call next() express agree to,
   *  else if call next('path') express redirect to path
   */

  /* to login is original intention? ok, i agree it */
  if (to.path === '/login') return next()
  /* get token, if it's null,express not login, to '/login'.
   * else if it's exist, ok, agree it
   */
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
